package com.ideafactory.client.br;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.ideafactory.client.utils.SpUtils;
import com.ideafactory.client.views.MenuToShow;

/**
 * Created by sanmu on 2016/5/21.
 */
public class BlueToothReceiver extends BroadcastReceiver {
    //private String BLUETOOTH="android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED";
    //private BluetoothAdapter mBluetoothAdapter;
    //private BluetoothDevice btDevice;
    private String btMessage="";
    //监听蓝牙状态

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.i("TAG---BlueTooth","接收到蓝牙状态改变广播！！");
        if(BluetoothDevice.ACTION_FOUND.equals(action))
        {
            btMessage=device.getName()+"设备已发现！！";
        }
        else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
        {
            try {
                String deviceName= SpUtils.getString(context,"bluename",null);
                btMessage=device.getName()+"设备已连接！！";
                if (device.getName().equals(deviceName)){
                    SpUtils.saveBoolean(context,"islinked",true);
                    if(MenuToShow.getInstance().blueInterface!=null){
                        MenuToShow.getInstance().blueInterface.BlueListen(true);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("---------"+device.getName()+"设备已连接！！");

        }

        else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action))
        {
            btMessage=device.getName()+"正在断开蓝牙连接。。。";
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
        {
            btMessage=device.getName()+"蓝牙连接已断开！！";

            System.out.println("---------"+device.getName()+"蓝牙连接已断开！！");
            String deviceName= SpUtils.getString(context,"bluename",null);
            if (device.getName().equals(deviceName)){
                SpUtils.saveBoolean(context,"islinked",false);
                if(MenuToShow.getInstance().blueInterface!=null){
                    MenuToShow.getInstance().blueInterface.BlueListen(false);
                }
            }


        }

    }

}
